package com.cqrt.aop;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.cqrt.mapper.PlanTemplateMappper;
import com.cqrt.vo.TyppmPlanActiveVO;
import com.hustcad.plm.pdm.core.response.TyppmResponseResult;
import com.hustcad.plm.rpm.model.entity.plan.TyppmPlanActive;
import lombok.extern.slf4j.Slf4j;
import oracle.sql.TIMESTAMP;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Aspect
@Component
@Slf4j
public class PublishContentAOP {

    @Resource
    private PlanTemplateMappper planTemplateMappper;

    @Around(value = "execution(* com.hustcad.plm.rpm.controller.plan.PlanController.getPublishContent(..))")
    Object getPublishContent(ProceedingJoinPoint joinPoint) throws Throwable {
        TyppmResponseResult<List<TyppmPlanActive>> typpmResponseResult =(TyppmResponseResult<List<TyppmPlanActive>>) joinPoint.proceed();
        List<TyppmPlanActive> data = typpmResponseResult.getData();
        if(CollUtil.isNotEmpty(data)){
            List<TyppmPlanActiveVO> typpmPlanActiveVOS = BeanUtil.copyToList(data, TyppmPlanActiveVO.class);
            String activityId = data.get(0).getOid();
            Map<String, TIMESTAMP> map = planTemplateMappper.selectPlanByActivityId(activityId);
            for (TyppmPlanActiveVO typpmPlanActive : typpmPlanActiveVOS) {
                List<String> strings = planTemplateMappper.selectPrePlanNameById(typpmPlanActive.getOid());
                StringBuilder builder= new StringBuilder();
                for (String string : strings) {
                    builder=builder.append(" "+string);
                }
                typpmPlanActive.setPlanStartDate(map.get("STARTDATE").dateValue());
                typpmPlanActive.setPlanEndDate(map.get("FINISHDATE").dateValue());
                typpmPlanActive.setPreTask(builder.toString());
            }
        }
        return typpmResponseResult;
    }
}
